﻿#ifndef WEBSOCKETNETWORKCENTER_H
#define WEBSOCKETNETWORKCENTER_H

#include <QMutex>
#include <QThread>
#include <QWebSocket>
#include <QWebSocketServer>

class WebSocketNetWorkCenter : public QThread
{
    Q_OBJECT
public:
    /**
    * 获取单例实例
    * @return 返回单例实例
    */
    static WebSocketNetWorkCenter* getInstance();
    /**
    * 删除单例实例
    */
    static void deleteInstance();
    /**
     * @brief sendMessage 发送消息
     * @param channel
     * @param message
     */
    void sendMessage(QString &message);
private:
    explicit WebSocketNetWorkCenter(QObject *parent = nullptr);
    ~WebSocketNetWorkCenter();
private:
    static WebSocketNetWorkCenter* instance;
    static QMutex mutex;
signals:
    void signal_send_message(QString &message);
private slots:
    void slot_send_message(QString &message);
    void handleNewConnection();
    void onSocketDisconnected();
//    void processTextMessage(QString message);
private:
    QWebSocketServer *m_server;

    QMutex clientMapMutex;
    QMap<QString, QWebSocket*> clientMap;
protected:
    void run() override;
};

#endif // WEBSOCKETNETWORKCENTER_H
